package com.villaday.idf.activity;

import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.model3.GuestPostEntity;
import com.villaday.idf.model3.MemberGuestChangeEntity;
import com.villaday.idf.model3.MemberGuestEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.StatusBarUtil;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class CommonAddActivity extends BaseActivity {


    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.tv_ca_name)
    TextView tvCaName;
    @BindView(R.id.tv_ca_name_left)
    TextView tvCaNameLeft;
    @BindView(R.id.et_ca_name)
    EditText etCaName;
    @BindView(R.id.vw_ca_name)
    View vwCaName;
    @BindView(R.id.tv_ca_name_en_left)
    TextView tvCaNameEnLeft;
    @BindView(R.id.et_ca_name_en_family)
    EditText etCaNameEnFamily;
    @BindView(R.id.et_ca_name_en_give)
    EditText etCaNameEnGive;
    @BindView(R.id.vw_ca_name_en)
    View vwCaNameEn;
    @BindView(R.id.rl_ca_name)
    RelativeLayout rlCaName;
    @BindView(R.id.tv_ca_card)
    TextView tvCaCard;
    @BindView(R.id.tv_ca_card_left)
    TextView tvCaCardLeft;
    @BindView(R.id.et_ca_card)
    EditText etCaCard;
    @BindView(R.id.vw_ca_card)
    View vwCaCard;
    @BindView(R.id.tv_ca_card_en_left)
    TextView tvCaCardEnLeft;
    @BindView(R.id.et_ca_card_en)
    EditText etCaCardEn;
    @BindView(R.id.vw_ca_card_en)
    View vwCaCardEn;
    @BindView(R.id.rl_ca_card)
    RelativeLayout rlCaCard;
    @BindView(R.id.tv_ca_phone_left)
    TextView tvCaPhoneLeft;
    @BindView(R.id.et_ca_phone)
    EditText etCaPhone;
    @BindView(R.id.ll_ca_phone)
    LinearLayout llCaPhone;
    @BindView(R.id.tv_ca_email_left)
    TextView tvCaEmailLeft;
    @BindView(R.id.et_email_phone)
    EditText etEmailPhone;
    @BindView(R.id.rl_ca_email)
    LinearLayout rlCaEmail;
    @BindView(R.id.tv_common_add)
    TextView tvCommonAdd;
    private Bundle mBundle;
    private MemberGuestEntity.BodyBean bodyBean;

    @Override
    protected int getViewResId() {
        return R.layout.activity_common_add;
    }


    @Override
    protected void init() {
        super.init();
        StatusBarUtil.setTranslucentForImageViewInFragment(this, null);
        StatusBarUtil.setLightMode(this);
//        if (PhoneUtil.getAndroidOSVersion() > 19) {
//            toolbarText.setPadding(0, StatusBarUtils.getStatusBarHeight(this), 0, 0);
//        }


        mBundle = getIntent().getExtras();
        String title = "新增常用旅客";
        if (null != mBundle) {
            title = "修改常用旅客";
            String data = mBundle.getString("data");
            bodyBean = new Gson().fromJson(data, MemberGuestEntity.BodyBean.class);

            if (bodyBean.getNameCn() != null) {
                etCaName.setText(bodyBean.getNameCn());
            }

            if (bodyBean.getFamilyNameEn() != null) {
                etCaNameEnFamily.setText(bodyBean.getFamilyNameEn());
            }

            if (bodyBean.getGivenNameEn() != null) {
                etCaNameEnGive.setText(bodyBean.getGivenNameEn());
            }

            if (bodyBean.getIdcard() != null) {
                etCaCard.setText(bodyBean.getIdcard());
            }

            if (bodyBean.getPassport() != null) {
                etCaCardEn.setText(bodyBean.getPassport());
            }

            if (bodyBean.getMobile() != null) {
                etCaPhone.setText(bodyBean.getMobile());
            }

            if (bodyBean.getEmail() != null) {
                etEmailPhone.setText(bodyBean.getEmail());
            }
        }

        ToolBarUtil.initToolBar(toolbarText, title, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        toolbarText.setBackgroundResource(R.color.colorBackground);
    }


    @OnClick({R.id.tv_common_add})
    public void btnOnclick(View view) {
        switch (view.getId()) {
            case R.id.tv_common_add:
                if (etCaName.getText().length() == 0 && (etCaNameEnGive.getText().length() == 0 || etCaNameEnFamily.getText().length() == 0)) {
                    ToastUtil.initToast(this, "请填写姓名");
                } else if (etCaCard.getText().length() == 0 && etCaCardEn.getText().length() == 0) {
                    ToastUtil.initToast(this, "请至少填写一个证件");
                } else if (etCaCard.getText().length() != 0 && etCaName.getText().length() == 0) {
                    ToastUtil.initToast(this, "请填写中文姓名");
                } else if (etCaCardEn.getText().length() != 0 && (etCaNameEnFamily.getText().length() == 0 || etCaNameEnGive.getText().length() == 0)) {
                    ToastUtil.initToast(this, "请填写完整英文姓名");
                } else {
                    GuestPostEntity guestPostEntity = new GuestPostEntity();
                    if (null != mBundle) {
                        guestPostEntity.setId(bodyBean.getId());
                    }
                    guestPostEntity.setNameCn(etCaName.getText().toString());
                    guestPostEntity.setAction(0);
                    guestPostEntity.setActionType(0);
                    guestPostEntity.setFamilyNameEn(etCaNameEnFamily.getText().toString());
                    guestPostEntity.setGivenNameEn(etCaNameEnGive.getText().toString());
                    guestPostEntity.setMobile(etCaPhone.getText().toString());
                    guestPostEntity.setEmail(etEmailPhone.getText().toString());
                    guestPostEntity.setIdcard(etCaCard.getText().toString());
                    guestPostEntity.setPassport(etCaCardEn.getText().toString());
                    guestPostEntity.setPreferred(true);
                    LogUtil.jLog().e(new Gson().toJson(guestPostEntity));
                    Map<String, Object> map = new HashMap<>();
                    OkHttpUtil.postJson(this, Constant.URL.memberGuest, guestPostEntity, map, new OkHttpUtil.OnDataListener() {
                        @Override
                        public void onResponse(String url, String json) {
                            LogUtil.jLog().e(json);
                            if (!TextUtils.isEmpty(json)) {
                                MemberGuestChangeEntity memberGuestChangeEntity = new Gson().fromJson(json, MemberGuestChangeEntity.class);
                                if (memberGuestChangeEntity.getCode().equals(Constant.Strings.SUC) && memberGuestChangeEntity.getBody().getStatus() == 1) {
                                    if (null != mBundle) {
                                        ToastUtil.initToast(CommonAddActivity.this, "修改成功！");
                                    }else {
                                        ToastUtil.initToast(CommonAddActivity.this, "添加成功！");
                                    }

                                    setResult(RESULT_OK);
                                    finish();
                                } else {
                                    ToastUtil.initToast(CommonAddActivity.this, memberGuestChangeEntity.getMsg());
                                }
                            }
                        }

                        @Override
                        public void onFailure(String url, String error) {

                        }
                    });
                }
                break;
        }
    }
}
